package com.netease.nim.uikit.business.session.actions;

import android.graphics.Color;
import android.view.View;

import com.netease.nim.uikit.R;
import com.netease.nim.uikit.business.util.Des3Util;
import com.netease.nim.uikit.impl.NimUIKitImpl;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.CheckModel;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.netease.nimlib.sdk.chatroom.ChatRoomMessageBuilder;
import com.netease.nimlib.sdk.msg.MessageBuilder;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.vondear.rxui.view.dialog.RxDialogGold;
import com.vondear.rxui.view.dialog.RxDialogSureCancel;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by hzxuwen on 2015/6/12.
 */
public class ImageAction extends PickImageAction {

    public ImageAction() {
        super(R.drawable.nim_message_plus_photo_selector, R.string.input_panel_photo, true);
    }

    @Override
    protected void onPicked(File file) {
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            if(baseBean.isSuccess()){
                IMMessage message;
                if (getContainer() != null && getContainer().sessionType == SessionTypeEnum.ChatRoom) {
                    message = ChatRoomMessageBuilder.createChatRoomImageMessage(getAccount(), file, file.getName());
                } else {
                    message = MessageBuilder.createImageMessage(getAccount(), getSessionType(), file, file.getName());
                    String result = (String)baseBean.getData();
                    String[] results = result.split("\\^\\^");
                    Map<String,Object> map = new HashMap<>();
                    map.put("money",results[1]);
                    message.setRemoteExtension(map);
                }
                sendMessage(message);
            }else{
                initDialog();
            }
        };
        CheckModel regModel = new CheckModel();//登录上传参数
        regModel.setMemberId(SharedHelper.readId(getContainer().activity));
        regModel.setOtherMemberId(getContainer().account);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(regModel));
        HttpMethods.getInstance().checkMsg(new ProgressSubscriber<>(onNextListener, getContainer().activity, true), requestBody);
    }

    private void initDialog(){
//        RxDialogSureCancel rxDialogEditSureCancel =new RxDialogSureCancel(getActivity());
//        rxDialogEditSureCancel.getLogoView().setVisibility(View.GONE);
//        rxDialogEditSureCancel.getTitleView().setTextSize(18);
//        rxDialogEditSureCancel.getTitleView().setTextColor(Color.BLACK);
//        rxDialogEditSureCancel.setTitle("提示");
//        rxDialogEditSureCancel.setSure("充值萤火币");
//        rxDialogEditSureCancel.getSureView().setTextColor(getActivity().getResources().getColor(R.color.orange));
//        rxDialogEditSureCancel.setCancel("取消");
//        rxDialogEditSureCancel.setContent("您的萤火币不足，请充值萤火币继续与对方畅聊！");
//        rxDialogEditSureCancel.setCancelListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                rxDialogEditSureCancel.dismiss();
//            }
//        });
//        rxDialogEditSureCancel.setSureListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                NimUIKitImpl.getSessionListener().openVipActivity(getActivity(),5);
////                RxActivityTool.skipActivity(getActivity(), VipCenterActivity.class);
//                rxDialogEditSureCancel.dismiss();
//            }
//        });
//        rxDialogEditSureCancel.show();
        RxDialogGold rxDialogEditSureCancel =new RxDialogGold(getActivity());
        rxDialogEditSureCancel.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NimUIKitImpl.getSessionListener().openVipActivity(getActivity(),10);
                rxDialogEditSureCancel.dismiss();
            }
        });
        rxDialogEditSureCancel.setSureListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NimUIKitImpl.getSessionListener().openVipActivity(getActivity(),5);
                rxDialogEditSureCancel.dismiss();
            }
        });
        rxDialogEditSureCancel.show();
    }
}

